package main.java.cn.wolfcode.service.impl;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void insert(Permission permission) {
        permissionMapper.insert(permission);

    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByPrimaryKey(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> selectAll() {
        List<Permission> list = permissionMapper.selectAll();
        return list;
    }
    
    @Override
    public PageInfo<Permission> queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.queryForList(qo);
        return new PageInfo<Permission>(permissions);
    }

    @Override
    public List<Permission> selectByRloeId(Long id) {
        List<Permission> permissions = permissionMapper.selectByRloeId(id);
        return permissions;
    }



    /**
     *  获取spring中所有的bean
     */
    @Autowired
    private ApplicationContext ctx;
    /**
     * 用处理映射器收集所有的映射路径及对应的处理方法前提是该方法上面必须有贴RequestMapping注解
     */
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;
    @Override
    public void reload() {
        //获取数据库中的所有权限表达式
        List<String> permissions = permissionMapper.selectAllExperssion();
        //利用Spring的处理器来获取所有的请求方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        //返回所有的values
        Collection<HandlerMethod> values = handlerMethods.values();
        //遍历所有的values
        for (HandlerMethod method : values) {
            RequiredPermission methodAnnotation = method.getMethodAnnotation(RequiredPermission.class);
            //判断是否有注解
            if (methodAnnotation == null) {
                continue;
            }
            //从注解中获取权限名称和表达式
            String name = methodAnnotation.name();
            String expression = methodAnnotation.expression();
            //判断当前方法的权限表达式在集合中是否已经存在
            if(!permissions.contains(expression)){
                //封装到权限对象并保存到数据
                Permission permission = new Permission();
                permission.setName(name);
                permission.setExpression(expression);
                permissionMapper.insert(permission);
                //并添加到集合达到数据库和集合的同步更新
                permissions.add(expression);

            }
        }
    }
    @Override
    public List<String> selectPermissionByEmpId(Long id) {
        List<String> expressions=permissionMapper.selectPermissionByEmpId(id);
        return expressions;
    }
}
